package com.dxt.mqtt;

import com.alibaba.fastjson.JSON;
import com.dxt.mqtt.utils.ServerMQTTUtil;
import com.dxt.service.CacheManager;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class pushliserTest {

    public static void main(String[] args) throws MqttException, UnsupportedEncodingException, InterruptedException {
        CacheManager cacheManager = new CacheManager();
        //设置推送消息体
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("relay1", "off");
        Map<String, Object> messageMap = new HashMap<String, Object>();
        messageMap.put("data", data);
        String MQTTObject = JSON.toJSONString(messageMap);
        System.out.println(MQTTObject);
        //将信息写入消息体
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setQos(1);
        mqttMessage.setRetained(true);
        mqttMessage.setPayload(MQTTObject.getBytes("UTF-8"));
        //根据deviceIdList内容进行topic设置和发送推送


        String topic = "100190700086/100190700086/set";

        ServerMQTTUtil serverMQTTUtil = new ServerMQTTUtil(topic,"server11",cacheManager);
        serverMQTTUtil.publish(mqttMessage);
        System.out.println(mqttMessage.isRetained() + "------ratained状态");

    }
}
